package MalouZoo;

import java.util.ArrayList;
import java.util.List;

public class Manager extends User implements Management,DisplayManager{
    private String name;
    private List<AnimalKeeper> animalKeepers = new ArrayList<>();
    private List<Animal> animals = new ArrayList<>();
    private List<Visitor> visitors = new ArrayList<>();

    public Manager(String name) {
        this.name = name;
    }

    // 管理饲养员
    public void addZookeeper(AnimalKeeper animalKeeper) {
        animalKeepers.add(animalKeeper);
    }

    public void removeZookeeper(AnimalKeeper animalKeeper) {
        animalKeepers.remove(animalKeeper);
    }

    public List<AnimalKeeper> getAnimalKeepers() {
        return animalKeepers;
    }

    @Override
    public boolean addAnimalKeeper(AnimalKeeper animalKeeper) {
        return this.animalKeepers.add(animalKeeper);
    }

    @Override
    public boolean deleteAnimalKeeper(AnimalKeeper animalKeeper) {
        return this.animalKeepers.remove(animalKeeper);
    }

    @Override
    public boolean deleteAnimalKeeper(String animalKeeperName) {
        return this.animalKeepers.remove(findAnimalKeeper(animalKeeperName));
    }

    @Override
    public boolean updateAnimalKeeper(AnimalKeeper oldAnimalKeeper, AnimalKeeper newAnimalKeeper) {
        boolean successedRemoved = this.animalKeepers.remove(oldAnimalKeeper);
        boolean successedAdded = this.animalKeepers.add(newAnimalKeeper);
        return (successedAdded && successedRemoved);
    }

    @Override
    public AnimalKeeper findAnimalKeeper(String animalKeeperName) {
        AnimalKeeper foundAnimalKeeper = null;
        //用foreach循环遍历
        for( AnimalKeeper animalKeeper : this.animalKeepers ){
            if (animalKeeper.getUserName().equals(animalKeeperName)) {
                foundAnimalKeeper = animalKeeper;
            }
        }
        return foundAnimalKeeper;
    }

    // 管理动物
    @Override
    public boolean addAnimal(Animal animal) {
        return this.animals.add(animal);
    }

    @Override
    public boolean deleteAnimal(Animal animal) {
        return this.animals.remove(animal);
    }

    @Override
    public boolean deleteAnimal(String animalName) {
        return this.animals.remove(findAnimal(animalName));
    }

    @Override
    public boolean updateAnimal(Animal oldAnimal, Animal newAnimal) {
        boolean successedRemoved = this.animals.remove(oldAnimal);
        boolean successedAdded = this.animals.add(newAnimal);
        return (successedAdded && successedRemoved);
    }

    @Override
    public Animal findAnimal(String animalName) {
        Animal foundAnimal = null;
        //用foreach循环遍历
        for( Animal animal : this.animals ){
            if (animal.getUserName().equals(animalName)) {
                foundAnimal= animal;
            }
        }
        return foundAnimal;
    }

    public void removeAnimal(Animal animal) {
        animals.remove(animal);
    }

    public List<Animal> getAnimals() {
        return animals;
    }

    //管理游客
    @Override
    public boolean addVisitor(Visitor visitor) {
        return this.visitors.add(visitor);
    }

    @Override
    public boolean deleteVisitor(Visitor visitor) {
        return this.visitors.remove(visitor);
    }

    @Override
    public boolean deleteVisitor(String visitorName) {
        return this.visitors.remove(findVisitor(visitorName));
    }

    @Override
    public boolean updateVisitor(Visitor oldVisitor, Visitor newVisitor) {
        boolean successedRemoved = this.visitors.remove(newVisitor);
        boolean successedAdded = this.visitors.add(newVisitor);
        return (successedAdded && successedRemoved);
    }

    @Override
    public Visitor findVisitor(String visitorName) {
        Visitor foundVisitor = null;
        //用foreach循环遍历
        for( Visitor visitor : this.visitors ){
            if (visitor.getUserName().equals(visitorName)) {
                foundVisitor = visitor;
            }
        }
        return foundVisitor;
    }

    public void removeVisitor(Visitor visitor) {
        visitors.remove(visitor);
    }

    public List<Visitor> getVisitors() {
        return visitors;
    }


    @Override
    public String displayAllAnimalKeepers() {
        String displayMessage = "";
        for( AnimalKeeper animalKeeper : this.animalKeepers ){
            displayMessage += animalKeeper.getUserName();
        }
        return displayMessage;
    }

    @Override
    public String displayAllAnimals() {
        String displayMessage = "";
        for( Animal animal : this.animals ){
            displayMessage += animal.getUserName();
        }
        return displayMessage;
    }

    @Override
    public String displayAllVisitors() {
        String displayMessage = "";
        for(Visitor visitor : this.visitors ){
            displayMessage += visitor.getUserName();
        }
        return displayMessage;
    }
}
